package gbench.appdemo.myworld.market.store;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import gbench.common.tree.LittleTree.IRecord;
import static gbench.common.tree.LittleTree.IRecord.*;


/**
 * 
 * @author gbench
 *
 */
public class DrcrTransactionDatabase{
    
    /**
     * 
     * @param onlineStore
     */
    public DrcrTransactionDatabase(OnlineStore onlineStore) {
        super();
        this.onlineStore = onlineStore;
    }

    public IRecord newRecord() {
        return REC("store", onlineStore);
    }
    
    /**
     * 
     * @param callbacks
     * @return
     */
    DrcrTransactionDatabase withTransaction(final Function<IRecord,Stream<IRecord>> callbacks) {
        final var rec = REC("store",this.onlineStore,"time",LocalDateTime.now());
        this.addAll(callbacks.apply(rec).collect(Collectors.toList()));
        return this;
    }
    
    public OnlineStore getOnlineStore() {
        return onlineStore;
    }

    public void setOnlineStore(OnlineStore onlineStore) {
        this.onlineStore = onlineStore;
    }
    
    public DrcrTransactionDatabase addAll(List<IRecord>rr) {
        this.cache.addAll(rr);
        return this;
    }
    
    /**
     * 
     * @return
     */
    public Stream<IRecord> getTransactions() {
        return this.cache.stream();
    }
    
    /**
     * 
     */
    
    private OnlineStore onlineStore;
    private List<IRecord> cache = new LinkedList<IRecord>();
}